﻿
namespace n_SysForm
{
using System;
using System.Windows.Forms;
using n_Compiler;
using n_ET;
using n_MainSystemData;
using n_ParseNet;
using n_Config;
using n_TargetFile;
using n_AVRdude;
using System.IO;

/// <summary>
/// Description of WorkForm.
/// </summary>
public partial class SysForm : Form
{
	//主窗口
	public SysForm()
	{
		//
		// The InitializeComponent() call is required for Windows Forms designer support.
		//
		InitializeComponent();
		
		//初始化
		if( G.Donsok ) {
			this.查看版本信息button.Visible = false;
		}
	}
	
	//显示
	public void Run()
	{
		this.Show();
		this.Activate();
	}
	
	void SetIDEFormFormClosing(object sender, FormClosingEventArgs e)
	{
		this.Visible = false;
		e.Cancel = true;
	}
	
	void 查看版本信息buttonClick(object sender, EventArgs e)
	{
		if( U.U.CopyRightBox == null ) {
			U.U.CopyRightBox = new n_CopyRightForm.CopyRightForm();
		}
		U.U.CopyRightBox.Run();
		
		this.Visible = false;
	}
	
	void Button系统设置Click(object sender, EventArgs e)
	{
		A.SetIDEBox.Run();
		
		this.Visible = false;
	}
	
	void Button模块库管理器Click(object sender, EventArgs e)
	{
		if( A.ModuleLibMngBox == null ) {
			A.ModuleLibMngBox = new linkboy.ModuleLibMng();
		}
		A.ModuleLibMngBox.Run();
		
		this.Visible = false;
	}
	
	void ButtonTempClick(object sender, EventArgs e)
	{
		FolderBrowserDialog fbd = new FolderBrowserDialog();
		//fbd.SelectedPath = "D:\Test";
		if(fbd .ShowDialog() == DialogResult.OK)
		{
			SaveNew.Start( fbd.SelectedPath );
		}
	}
	
	int Tick = 0;
	void 查看版本信息buttonMouseDown(object sender, MouseEventArgs e)
	{
		if(e.Button == MouseButtons.Right ) {
			
			Tick++;
			if( Tick > 5 ) {
				this.FormBorderStyle = FormBorderStyle.Sizable;
			}
		}
	}
	
	void ButtonDebugClick(object sender, EventArgs e)
	{
		G.CompileMessageBox.Run();
	}
	
	//-----------------------------------------------------------------------
	//注意，遍历文件夹每次最多不要超过300个文件，否则报错
	static class SaveNew
	{
		static bool Exist;
		
		static int Index;
		
		//开始
		public static void Start( string Path )
		{
			Exist = false;
			B_FindFile( Path );
		}
		
		//遍历指定的文件夹中的全部文件
		static void B_FindFile(string sSourcePath )
		{
			if( Exist ) {
				return;
			}
			
			//在指定目录及子目录下查找文件,在list中列出子目录及文件
			DirectoryInfo Dir = new DirectoryInfo( sSourcePath );
			DirectoryInfo[] DirSub = Dir.GetDirectories();
			
			//遍历当前文件夹中的所有文件
			foreach( FileInfo f in Dir.GetFiles("*.*", SearchOption.TopDirectoryOnly) ) {
				if( Exist ) {
					return;
				}
				string FilePath = Dir + @"\" + f.ToString();
				B_DealFile( FilePath );
			}
			//遍历所有的子文件夹
			foreach( DirectoryInfo d in DirSub ) {
				if( Exist ) {
					return;
				}
				
				//防护对话框
				//DialogResult dr = MessageBox.Show( "需要处理新文件夹吗?\n" + d, "文件夹处理", MessageBoxButtons.YesNo );
				//if( dr == DialogResult.No ) {
				//	Exist = true;
				//	return;
				//}
				
				B_FindFile(Dir + @"\" + d.ToString());
			}
		}
		
		//处理文件
		static void B_DealFile( string FileName )
		{
			if( !FileName.EndsWith( ".lab" ) ) {
				return;
			}
			
			
			Index += 1;
			DialogResult dr = MessageBox.Show( Index + " 需要处理此文件吗?\n" + FileName, "文件处理", MessageBoxButtons.YesNoCancel );
			if( dr == DialogResult.Cancel ) {
				Exist = true;
				return;
			}
			else if( dr == DialogResult.No ) {
				return;
			}
			
			
			
			//string NewName = FileName.Remove( FileName.Length - 3 ) + "lab";
			//MessageBox.Show( "<" + NewName + ">" );
			//File.Move( FileName, NewName );
			
			U.U.m.OpenFile( true, FileName );
		}
	}
}
}




